package com.kgc.warehouse.bussness.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kgc.warehouse.bussness.entity.Provider;
import com.kgc.warehouse.bussness.service.ProviderService;
import com.kgc.warehouse.bussness.vo.ProviderVo;
import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.ResponseResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("provider")
public class ProviderController {

    @Resource
    private ProviderService providerService;

    //供应商名称下拉列表
    @RequestMapping("loadAllProviderForSelect")
    public ResponseResult loadAllProviderForSelect(){
        LambdaQueryWrapper<Provider> providerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        providerLambdaQueryWrapper.eq(Provider::getAvailable,1);
        List<Provider> list = providerService.list(providerLambdaQueryWrapper);
        return new ResponseResult(list,null, CodeMsg.SUCCESS);
    }

    @RequestMapping("loadAllProvider")
    public ResponseResult loadAllProvider(ProviderVo providerVo){
        //1.声明一个分页page对象
        IPage<Provider> page = new Page(providerVo.getPage(),providerVo.getLimit());
        //2.声明一个queryWrapper
        QueryWrapper<Provider> queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(providerVo.getProvidername()),"providername",providerVo.getProvidername());
        queryWrapper.like(StringUtils.isNotBlank(providerVo.getConnectionperson()),"connectionperson",providerVo.getConnectionperson());

        providerService.page(page,queryWrapper);
        return new ResponseResult(page.getRecords(),page.getTotal(), CodeMsg.SUCCESS);
    }


    @RequestMapping("addProvider")
    public ResponseResult addCustomer(ProviderVo ProviderVo){
        boolean save = providerService.save(ProviderVo);
        return new ResponseResult(CodeMsg.ADD_SUCCESS);
    }

    @RequestMapping("updateProvider")
    public ResponseResult updateCustomer(ProviderVo ProviderVo){
        try {
            providerService.updateById(ProviderVo);
            return new ResponseResult(CodeMsg.UPDATE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new  ResponseResult(CodeMsg.UPDATE_FAIL);
        }
    }



    @RequestMapping("deleteProvider")
    public ResponseResult deleteProvider(Integer id){
        try {
            providerService.deleteProviderById(id);
            return new ResponseResult(CodeMsg.DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CodeMsg.DELETE_FAIL);
        }
    }
}
